package example.com.zyfypt_618zgm.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.util.SortedList;
import retrofit2.Call;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import example.com.zyfypt_618zgm.R;
import example.com.zyfypt_618zgm.model.RegisterModel;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 小茗同学 on 2017/5/8.
 */

public class Zhuce extends AppCompatActivity implements View.OnClickListener{
    private Button button4;
    private Button button5;
    private EditText edit3;
    private EditText edit4;
    private EditText edit5;
    private EditText edit7;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zhucexiangduibuju);
        edit3=(EditText) findViewById(R.id.editText3);
        edit4=(EditText) findViewById(R.id.editText4);
        edit5=(EditText) findViewById(R.id.editText5);
        edit7=(EditText) findViewById(R.id.editText7);
        Button button=(Button)findViewById(R.id.button4);
        Button button2=(Button)findViewById(R.id.button5);
        button.setOnClickListener(this);
        button2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button4:
                if(edit3.getText().toString().isEmpty()||edit3.getText().toString().isEmpty()){
                    Toast.makeText(this,"注册失败！请检查是否输入了用户名或密码",Toast.LENGTH_SHORT).show();
                }
                else if(!edit4.getText().toString().equals(edit4.getText().toString())) {
                    Toast.makeText(this,"注册失败！请检查两次密码输入是否一致",Toast.LENGTH_SHORT).show();
                }else
                {
                    RegisterModel registerModel = new RegisterModel();
                    Call<String> call=registerModel.getResult(edit3.getText().toString(), edit4.getText().toString(), edit7.getText().toString());
                    call.enqueue(new Callback<String>() {
                        @Override
                        public void onResponse(Call<String> call, Response<String> response) {
                            if(response.isSuccessful() && response.body() != null){
                                if(response.body().equals("1")){
                                    Toast.makeText(Zhuce.this,"注册成功",Toast.LENGTH_SHORT).show();
                Intent main=new Intent();
                main.setClass(Zhuce.this,Denglu.class);
                startActivity(main);

                                }
                                else {
                                    Toast.makeText(Zhuce.this, "用户名已存在", Toast.LENGTH_SHORT).show();
                                }
                            } else {
                                Toast.makeText(Zhuce.this,"连接服务器失败",Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<String> call, Throwable t) {
                            Toast.makeText(Zhuce.this,"注册失败",Toast.LENGTH_SHORT).show();
                        }
                    });
                }
                break;
            case R.id.button5:
                Toast.makeText(this,"返回登录",Toast.LENGTH_SHORT).show();
                finish();
                break;
            default:break;
        }
    }
}
